<template>
  <div class="login_container">
    <el-row class="header_title"> V0.x </el-row>
    <el-row class="center_area">
      <div class="login_area">
        <el-form ref="form" :rules="rules" :model="loginForm">
          <h3 class="login_title">h5-</h3>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              prefix-icon="el-icon-s-custom"
              auto-complete="off"
              placeholder="账号"
              @keyup.enter.native="submitClick"
              tabindex="1"
              autofocus
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              show-password
              v-model="loginForm.password"
              prefix-icon="el-icon-key"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="submitClick"
              tabindex="1"
            ></el-input>
          </el-form-item>
          <el-form-item style="width: 100%">
            <el-button type="primary" style="width: 100%" @click="submitClick"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-row>
  </div>
</template>
<script>
let Base64 = require("js-base64").Base64;

export default {
  data() {
    return {
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      loginForm: {
        username: "",
        password: "",
      },
    };
  },
  methods: {
    async submitClick() {
      let valid = await this.$refs.form.validate().catch(() => {});
      if (!valid) return;
      await this.login(this.loginForm.loginName, this.loginForm.passWord);
    },
    async login(username, password) {
      let res = await this.$axios.post("/user/login", this.loginForm);
      if (res.data.code == 0) {
        var _t = JSON.parse(Base64.decode(res.data.data));
        this.$cookies.set("_t", res.data.data, "1d");
        this.$cookies.set("u", _t.username, "1d");
        this.$cookies.set("r", _t.role, "1d");
        this.$router.replace("/");
      } else {
        this.$message.error(res.data.msg);
      }
    },
  },
};
</script>
<style scoped>
.login_container {
  width: 100%;
  height: 100%;
  position: fixed;
  background-image: url("../../../assets/image/basks.jpg");
  background-position: 50% 50%;
  background-size: cover;
  background-repeat: no-repeat;
}

.header_title {
  width: 100%;
  height: 195px;
  font-size: 2.4rem;
  padding-top: 58px;
  text-shadow: 0 1px 1px #ddd, 0 2px 1px #deebfc, 0 8px 5px rgba(0, 0, 0, 0.3);
  color: #fff;
  text-align: center;
}

.logo_img {
  height: 55px;
  width: 80px;
  position: relative;
  top: 14px;
}

.center_area {
  width: 100%;
}

.login_area {
  border-radius: 15px;
  margin: 30px auto;
  width: 380px;
  height: 200px;
  padding: 60px 28px 120px 28px;
  background: #fff;
  box-shadow: 0 0 25px rgb(34, 34, 34);
  position: relative;
}

.login_title {
  margin: 0 auto 40px auto;
  text-align: center;
  font-size: 25px;
  font-weight: 400;
}

.el-form-item {
  margin-bottom: 30px;
}
</style>